package com.win;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigurationExcludeFilter;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.TypeExcludeFilter;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;

/** 
 * @description: 这是一个springboot应用 主程序类
 * @author win
 * @date: 2025/2/21 16:51
 */ 
//@SpringBootApplication
//@ComponentScan(value = "com.test")
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = { @ComponentScan.Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
        @ComponentScan.Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public class Main {
    public static void main(String[] args) {
        //1、返回我们IOC容器
        ConfigurableApplicationContext run = SpringApplication.run(Main.class, args);

        //2、查看容器里面的组件
//        String[] names = run.getBeanDefinitionNames();
//        for (String name : names) {
//            System.out.println(name);
//        }
//        //3、从容器中获取组件 == true
//        Pet tom01 = run.getBean("tom", Pet.class);
//        Pet tom02 = run.getBean("tom", Pet.class);
//        System.out.println("组件tom："+(tom01 == tom02));
//
//        //4、com.atguigu.boot.config.MyConfig$$EnhancerBySpringCGLIB$$51f1e1ca@1654a892
//        // CGLIB代理对象
//        //配置类本身也是组件
//        MyConfig bean = run.getBean(MyConfig.class);
//        System.out.println(bean);
////
//        //如果@Configuration(proxyBeanMethods = true)代理对象调用方法。
//        //SpringBoot总会检查这个组件是否在容器中有。
//        //true 保持组件单实例
//        User user = bean.user01();
//        User user1 = bean.user01();
//        System.out.println("组件user:"+ (user == user1));
//
//        Cat tom = run.getBean("tom", Pet.class);
//
//        System.out.println("用户的宠物："+(user.getCat() == tom));
//
//        System.out.println("=================");
//        //5、获取组件
//        String[] beanNamesForType = run.getBeanNamesForType(User.class);
//        for (String s : beanNamesForType) {
//            System.out.println(s);
//        }
//
//        DBHelper bean1 = run.getBean(DBHelper.class);
//        System.out.println(bean1);

//        测试@Conditional

        boolean tom = run.containsBean("tom");
        System.out.println("容器中Tom组件："+tom);//false

        boolean user01 = run.containsBean("user01");
        System.out.println("容器中user01组件："+user01);//true

        boolean tom22 = run.containsBean("tom22");
        System.out.println("容器中tom22组件："+tom22);//true

        //测试 @importResource
        System.out.println("容器中userXml组件"+run.containsBean("userXml"));
    }
}